<template>
  <div>
    <Form inline :label-colon="true" class="search-form" :label-width="120">
      <FormItem label="备份文件名称">
        <Input v-model="params.backupName" placeholder="请输入备份文件名称"/>
      </FormItem>
      <FormItem :label-width="0">
        <Button
          type="primary"
          @click="publicGetData()"
          style="margin-right: 10px"
          v-auth="'列表'"
        >搜索
        </Button>
        <Button
         type="primary"
         @click="handleBackupFile(0)" v-auth="'新增'">完整备份</Button>
        <Button
         type="primary"
          @click="handleBackupFile(1)"
          style="margin-left: 10px;margin-right: 10px;"
          v-auth="'新增'"
          >增量备份</Button>
        <Button type="primary" @click="handleBackupFile(2)" style="margin-right: 10px;">数据结构备份</Button>
        <BaseUpload
          buttonText="导入"
          name="file"
          v-auth="'导入'"
          :show-upload-list="false"
          :action="action.importBackup"
          :format="fileFormat"
          @publicSuccess="publicSuccess"
          :maxSize="maxSize"
        />
      </FormItem>
    </Form>
    <Breadcrumb></Breadcrumb>
    <BaseTable
      ref="baseTable"
      :public-btn="['下载', {
        label: '还原',
        value: '还原',
        style: 'color:#FE4900'
      }]"
      @handle-expand="handleExpand"
      :action="action.readBackup"
      :columns="columns"
    />
  </div>
</template>

<script>
import { tableMixin } from '../../../mixin'
import API_KEY from '../../../api'
import columns from './backup-columns'
import { backupData, backupRestore } from '../../../api/modules/system-log/backup'
import message from '../../../utils/message'
import BaseUtil from '../../../utils/BaseUtil'
export default {
  name: 'Backup',
  mixins: [tableMixin],
  data () {
    return {
      params: {
        backupName: ''
      },
      menuCode: 'log-backup',
      action: {
        readBackup: API_KEY.log.readBackup,
        importBackup: API_KEY.log.importBackup
      },
      // 支持上传的文件后缀列表
      fileFormat: ['sql', 'log'],
      columns,
      maxSize: 10 * 1024
    }
  },
  methods: {
    /**
     * 备份
     * @param val
     */
    handleBackupFile (val) {
      backupData(val).then((data) => {
        message.message.success(data.message)
        this.publicGetData()
      })
    },
    /**
     * 文件上传成功触发
     * @param params 上传成功后返回数据
     */
    publicSuccess (params) {
      this.publicGetData()
    },
    /**
     * 表格中，除查看/删除/更改操作外，其他操作都将触发当前函数
     */
    handleExpand (type, row) {
      if (type === '下载') {
        this.handleDownload(row)
      } else if (type === '还原') {
        message.modal.confirm('还原操作', '你确定要还原吗？', () => {
          this.handleReduce(row)
        })
      }
    },
    /**
     * 点击下载触发
     * @param params
     */
    handleDownload (params) {
      BaseUtil.downBackupFile(params.backupPath)
    },
    /**
     * 点击还原触发
     * @param params
     */
    handleReduce (params) {
      backupRestore(params.id).then((data) => {
        message.message.success(data.message)
        this.publicGetData()
      })
    }
  },
  components: {
    BaseUpload: () => import('@/components/view-ui/BaseUpload')
  }
}
</script>

<style scoped>

</style>
